package com.lz.fruitday.fragment;


import android.database.DataSetObserver;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lz.fruitday.R;
import com.lz.fruitday.adapter.CarAdapter;
import com.lz.fruitday.bean.Good;

import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 */
public class ShopCarFragment extends Fragment {

    private ArrayList<Good> goods = new ArrayList<>();
    private CarAdapter carAdapter;
    private ListView lvRly;
    private TextView tvAll;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shop_car, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initData() {
        carAdapter.registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                tvAll.setText(String.valueOf(getSum()));
            }
        });
    }

    private int getSum() {
        int sum = 0;
        for (Good good : goods
                ) {
            sum+=good.getNum()*Integer.parseInt(good.getPrice());
        }

        return sum;
    }

    private void initView(View view) {
        lvRly = (ListView) view.findViewById(R.id.lv_cart);
        //ImageView imageView= (ImageView) view.findViewById(R.id.iv_empty);
        // lvRly.setEmptyView(imageView);
        tvAll = (TextView) view.findViewById(R.id.tv_all);
        carAdapter = new CarAdapter(goods, getActivity());
        lvRly.setAdapter(carAdapter);
    }


}
